// import proFetch from "../../../../../public/functions/project-fetch";
import {hrAjax as ajax} from 'src/hrpub/common/utils/utils';
import updateButtonStatus from "./updateButtonStatus";
import {cacheTools, toast} from 'nc-lightapp-front';

export default function getMainTableData(conditions) {
    const {orgVal, agreementType, pageInfo, refreshFlag, json } = this.state;
    
    return ajax({
        url: '/nccloud/hrcm/agreement/ChangeQueryAction.do',
        body: {
            pk_org: orgVal && orgVal.refpk ? orgVal.refpk : null,
            agreementType: agreementType,
            pageInfo: pageInfo,
            pageCode: '60110503p',
            ...conditions
        },
    }).then((res) => {
        let tableid = agreementType;
        let data = res.data;
        if (res.success) {
            let dataList;
            if (data && data.dataList) {
                if (data.dataList.postcont) {
                    dataList = data.dataList.postcont;
                } else if (data.dataList.secrerycont) {
                    dataList = data.dataList.secrerycont;
                } else if (data.dataList.traincont) {
                    dataList = data.dataList.traincont;
                } else{
                    tableid = agreementType + 'List';
                    dataList = data.dataList[tableid];
                }
            }
            if(data && !!data.formrelation){
                let template = this.props.meta.getMeta();
                template.formrelation = data.formrelation;
                this.props.meta.setMeta(template)
            }

            if ( !dataList ) {
                if(agreementType !== 'postcont' &&  agreementType !== 'secrerycont' && agreementType !== 'traincont'){
                    tableid = agreementType + 'List';
                }
                dataList = {
                    allpks: [],
                    areacode: agreementType,
                    rows: [],
                };
            }

            //给CardBefore添加_old 目的使cardform可编辑
            let meta = this.props.meta.getMeta();
            let tempMeta = meta[agreementType+'CardBefore'];
            !!tempMeta && !!tempMeta.items.length && tempMeta.items.map((item, index)=>{
                if( item.attrcode.indexOf('_old') == -1 ){
                    item.attrcode = item.attrcode + '_old';
                }
            })
            this.props.meta.setMeta(meta)
            setBoolean(dataList.rows);
            let allPks = dataList.rows.map(function (item) {
                return item.values.pk_agreement.value
            })
            cacheTools.set('allpks', allPks);
            this.props.editTable.setTableData( tableid , dataList);
            updateButtonStatus.call(this, orgVal, tableid);
            this.setState({
                pageInfo: dataList.pageInfo || pageInfo,
                pk_org: orgVal && orgVal.refpk ? orgVal.refpk : null,
                isVisiblePagination: !!dataList && dataList.rows.length ? true : false
            });
            if (refreshFlag) {
                toast({
                    color: 'success',
                    content: json['cm6011-000006'] //刷新成功！
                })
            }
        }
    });

    function setBoolean(rows) {
        for (let row in rows) {
            let data = rows[row].values;
            if( data.isrefer ){
                data.isrefer.display = data.isrefer && data.isrefer.value ? json['cm6011-000057'] : json['cm6011-000058']; //是-否
            }
            if( data.ifprop ){
                data.ifprop.display = data.ifprop && data.ifprop.value ? json['cm6011-000057'] : json['cm6011-000058']; //是-否
            }
        }
    }
}
